$(function(){
		var data = {};
		var pageManager = {
			$container: $('.js_container'),
			_pageStack: [],
			_configs: [],
			_defaultPage: null,
			_isGo: false,
			default: function (defaultPage) {
				this._defaultPage = defaultPage;
				return this;
			},
			init: function () {
				var self = this;

				$(window).on('hashchange', function (e) {

					var _isBack = !self._isGo;
					self._isGo = false;
					if (!_isBack) {
						return;
					}

					var url = location.hash.indexOf('#') === 0 ? location.hash : '#';
					var found = null;
					for(var i = 0, len = self._pageStack.length; i < len; i++){
						var stack = self._pageStack[i];
						if (stack.config.url === url) {
							found = stack;
							break;
						}
					}
					if (found) {
						self.back();
					}
					else {
						goDefault();
					}
				});

				function goDefault(){
					var url = location.hash.indexOf('#') === 0 ? location.hash : '#';
					var page = self._find('url', url) || self._find('name', self._defaultPage);
					self.go(page.name);
				}

				goDefault();

				return this;
			},
			push: function (config) {
				this._configs.push(config);
				return this;
			},
			go: function (to) {
				var config = this._find('name', to);

				if (!config) {
					return;
				}
				var html = $(config.template).html();
				var $html = $(html).addClass('slideIn').addClass(config.name);
				this.$container.append($html);
				this._pageStack.push({
					config: config,
					dom: $html
				});

				this._isGo = true;
				location.hash = config.url;

				if (!config.isBind) {
					this._bind(config);
				}

				return this;
			},
			back: function () {
				var stack = this._pageStack.pop();
				if (!stack) {
					return;
				}

				stack.dom.addClass('slideOut').on('animationend', function () {
					stack.dom.remove();
				}).on('webkitAnimationEnd', function () {
					stack.dom.remove();
				});

				return this;
			},
			_find: function (key, value) {
				var page = null;
				for (var i = 0, len = this._configs.length; i < len; i++) {
					if (this._configs[i][key] === value) {
						page = this._configs[i];
						break;
					}
				}
				return page;
			},
			_bind: function (page) {
				var events = page.events || {};
				for (var t in events) {
					for (var type in events[t]) {
						this.$container.on(type, t, events[t][type]);
					}
				}
				page.isBind = true;
			}
		};


		var register = {
			name: 'register',
			url: '#',
			template: '#tpl_reg1',
			events: {
				'.js_grid': {
					click: function (e) {
						var chec = $('#reg_protocol').attr('checked');
						if(!chec){
							mui.alert('请查看并勾选用户注册协议');
							return false;
						}

						var id = $(this).data('id');
						var checked = true;
						$("input").each(function(){
							var value = $(this).val();
							// if($.trim(value) == ''){
							// 	checked = false;
							// 	$.alert($(this).attr('placeholder'), function(){
							// 		$(this).focus();
							// 	});
							// 	return false;
							// }
							var r = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
							if (!r.test($("#phone").val())){
								mui.alert('手机号码不正确，请输入正确的手机号码');
								checked = false;
								return false;
							}
							data[$(this).attr('name')] = value;
						});
						if(!checked){
							return ;
						}
						console.log(data);

						$.post("/api/Main/register", data, function(e){
							//$.hideIndicator();
							// $.alert(e.info, '温馨提示', function() {
								if (e.error == 1){
									mui.alert(e.msg);
								}else if(e.error == 0){
									// location.href ='https://app.kayuhome.cn';
									mui.alert(e.msg);

								}
							// });
						});
						// $.ajax({
						// 	url: "/",	
						// 	type: "POST",
						// 	dataType: 'json',
						// 	data: data,
						// 	success: function(data) {
						// 		alert(data);
						// 		// $.hideIndicator();
						// 		$.alert(data.info, '温馨提示', function() {
						// 			if (data.status == 1){
						// 				location.href ='https://app.kayuhome.cn';
						// 			}
						// 		});
						// 	},
						// 	error: function(){
						// 		alert('123');
						// 	}

						// });
						
					}
				}
			}
		};
		
		//执行方法

		pageManager.push(register)
			.default('app_register')
			.init();

	});